/*
 * MIT License
 *
 * Copyright (c) 2019 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package com.wendy.common.core.utils;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;

/**
 * 通用工具
 *
 * @author by Mr.css on 2018/5/22.
 */
public class CommonUtils {
    private CommonUtils() {
    }

    /**
     * SpringMVC转发Key
     */
    public static String redirect(String uri) {
        return "redirect:" + uri;
    }

    /**
     * SpringMVC重定向Key
     */
    public static String forward(String uri) {
        return "forward:" + uri;
    }

    /**
     * 是否为null
     *
     * @param obj -
     * @return true/false
     */
    public static boolean isNull(Object obj) {
        return obj == null;
    }

    /**
     * 是否不为null
     *
     * @param obj -
     * @return true/false
     */
    public static boolean isNotNull(Object obj) {
        return obj != null;
    }

    /**
     * 是否为空
     *
     * @param str -
     * @return true/false
     */
    public static boolean isEmpty(String str) {
        return str == null || str.isEmpty();
    }

    /**
     * 是否为空
     *
     * @param str -
     * @return true/false
     */
    public static boolean isNotEmpty(String str) {
        return (str != null && str.length() != 0);
    }

    /**
     * 是否为空
     *
     * @param l -
     * @return true/false
     */
    public static boolean isEmpty( Collection l) {
        return l == null || l.isEmpty();
    }

    /**
     * 是否为空
     *
     * @param l -
     * @return true/false
     */
    public static boolean isNotEmpty(Collection l) {
        return l != null && !l.isEmpty();
    }

    /**
     * 是否为空
     *
     * @param map -
     * @return true/false
     */
    public static boolean isEmpty( Map map) {
        return map == null || map.isEmpty();
    }

    /**
     * 是否为空
     *
     * @param map -
     * @return true/false
     */
    public static boolean isNotEmpty( Map map) {
        return map != null && !map.isEmpty();
    }

    /**
     * 是否为空
     *
     * @param pObj -
     * @return true/false
     */
    public static boolean isEmpty(Object pObj) {
        if (pObj == null) {
            return true;
        } else if (pObj instanceof String) {
            return ((String) pObj).trim().isEmpty();
        } else if ((pObj instanceof Collection)) {
            return ((Collection<?>) pObj).isEmpty();
        } else if ((pObj instanceof Map)) {
            return ((Map<?, ?>) pObj).isEmpty();
        } else if (pObj.getClass().isArray()) {
            return Array.getLength(pObj) == 0;
        } else {
            return false;
        }
    }

    /**
     * 是否不为空
     *
     * @param pObj -
     * @return true/false
     */
    public static boolean isNotEmpty(Object pObj) {
        return !isEmpty(pObj);
    }

    /**
     * 给出1个值，如果为空，则使用另一个
     *
     * @param src    原值
     * @param newObj 默认值
     * @param <T>    泛型
     * @return src/newObj
     */
    public static <T> T nvl(T src, T newObj) {
        return isEmpty(src) ? newObj : src;
    }
}
